package com.tencent.sr.iris.activity.common.util;

import com.tencent.sr.rmall.common.exception.code.ErrorCode;
import com.tencent.sr.rmall.common.exception.retail.TRetailBizException;
import org.springframework.util.ObjectUtils;

/**
 * @author nixiaolin
 * @date 2022/7/22 9:47
 */
public abstract class AssertUtils {

    /**
     * 断言为空
     *
     * @param obj
     * @param errorCode
     */
    public static void isEmpty(Object obj, ErrorCode errorCode) {
        if (!ObjectUtils.isEmpty(obj)) {
            throw new TRetailBizException(errorCode);
        }
    }

    /**
     * 断言非空
     *
     * @param obj
     * @param errorCode
     */
    public static void isNotEmpty(Object obj, ErrorCode errorCode) {
        if (ObjectUtils.isEmpty(obj)) {
            throw new TRetailBizException(errorCode);
        }
    }

    /**
     * 断言为null
     *
     * @param obj
     * @param errorCode
     */
    public static void isNull(Object obj, ErrorCode errorCode) {
        if (obj != null) {
            throw new TRetailBizException(errorCode);
        }
    }

    /**
     * 断言为null
     *
     * @param obj
     * @param errorCode
     */
    public static void isNotNull(Object obj, ErrorCode errorCode) {
        if (obj == null) {
            throw new TRetailBizException(errorCode);
        }
    }

    /**
     * 断言为false
     *
     * @param expression
     * @param errorCode
     */
    public static void isFalse(boolean expression, ErrorCode errorCode) {
        if (expression) {
            throw new TRetailBizException(errorCode);
        }
    }

    /**
     * 断言equals
     *
     * @param o1
     * @param o2
     * @param errorCode
     */
    public static <T> void isEquals(T o1, T o2, ErrorCode errorCode) {
        if (o1 == null || o2 == null || !o1.equals(o2)) {
            throw new TRetailBizException(errorCode);
        }
    }

    /**
     * 断言equals不成立
     *
     * @param o1
     * @param o2
     * @param errorCode
     */
    public static <T> void isNotEquals(T o1, T o2, ErrorCode errorCode) {
        if (o1 != null && o2 != null && o1.equals(o2)) {
            throw new TRetailBizException(errorCode);
        }
    }

    /**
     * 断言 c1大于 c2
     *
     * @param c1
     * @param c2
     * @param errorCode
     */
    public static <T> void isGreaterThan(Comparable<T> c1, T c2, ErrorCode errorCode) {
        if (c1 == null || c2 == null) {
            throw new TRetailBizException(errorCode);
        }
        if (c1.compareTo(c2) <= 0) {
            throw new TRetailBizException(errorCode);
        }
    }

    /**
     * 断言c1小于c2
     *
     * @param c1
     * @param c2
     * @param errorCode
     */
    public static <T> void isLessThan(Comparable<T> c1, T c2, ErrorCode errorCode) {
        if (c1 == null || c2 == null) {
            throw new TRetailBizException(errorCode);
        }
        if (c1.compareTo(c2) >= 0) {
            throw new TRetailBizException(errorCode);
        }
    }

    /**
     * 断言c1小于等于c2
     *
     * @param c1
     * @param c2
     * @param errorCode
     */
    public static <T> void isLessEqual(Comparable<T> c1, T c2, ErrorCode errorCode) {
        if (c1 == null || c2 == null) {
            throw new TRetailBizException(errorCode);
        }
        if (c1.compareTo(c2) > 0) {
            throw new TRetailBizException(errorCode);
        }
    }

    /**
     * 断言 c1大于等于 c2
     *
     * @param c1
     * @param c2
     * @param errorCode
     */
    public static <T> void isGreaterEquals(Comparable<T> c1, T c2, ErrorCode errorCode) {
        if (c1 == null || c2 == null) {
            throw new TRetailBizException(errorCode);
        }
        if (c1.compareTo(c2) < 0) {
            throw new TRetailBizException(errorCode);
        }
    }

    /**
     * 断言 c1等于 c2
     *
     * @param c1
     * @param c2
     * @param errorCode
     */
    public static <T> void isEqual(Comparable<T> c1, T c2, ErrorCode errorCode) {
        if (c1 == null || c2 == null) {
            throw new TRetailBizException(errorCode);
        }
        if (c1.compareTo(c2) != 0) {
            throw new TRetailBizException(errorCode);
        }
    }

    /**
     * 断言 c1不等于 c2
     *
     * @param c1
     * @param c2
     * @param errorCode
     */
    public static <T> void isNotEqual(Comparable<T> c1, T c2, ErrorCode errorCode) {
        if (c1 == null || c2 == null) {
            throw new TRetailBizException(errorCode);
        }
        if (c1.compareTo(c2) == 0) {
            throw new TRetailBizException(errorCode);
        }
    }

    /**
     * 判布尔型
     *
     * @param expression
     * @param errorCode
     */
    public static void isTrue(boolean expression, ErrorCode errorCode) {
        if (!expression) {
            throw new TRetailBizException(errorCode);
        }
    }
}
